<?php

namespace app\admin\service\pay;

use app\admin\model\pay\BoxRechargePayType;
use app\admin\service\BaseService;

class BoxRechargePayTypeService extends BaseService
{

    public function __construct()
    {
        $this->model = new BoxRechargePayType();
    }

    /**
     * Desc : getList
     * User : Steven
     * Date : 2024-05-28 14:00
     * @return array
     */
    public function getList(): array
    {
        $params = request()->param();
        $where  = [];
        $name = $params['name'] ?? "";
        if (!empty($name)) {
            $where[] = ['g.name', 'like', '%' . $name . '%'];
        }

        $arr   = $this->model->alias('g')
            ->leftJoin('cq_box_recharge_payment_account b', 'b.id = g.payment_id')
            ->field('g.*,b.company')
            ->where($where)
            ->paginate()->toArray();
        // $arr['data'] = array_map(function ($item) {
        //     $item['extends'] = json_decode($item['extends'], true);
        //     return $item;
        // }, $arr['data']);
        return [
            'code'  => 0,
            'msg'   => 'success',
            'count' => $arr['total'] ?? 0,
            'data'  => $arr['data'] ?? [],
        ];
    }

    public function edit()
    {
        $params = request()->param();
        if (isset($params['extends'])) {
            $params['extends'] = json_encode($params['extends']);
        }
        return parent::newEdit($params,false,true); // TODO: Change the autogenerated stub
    }

    public function delete(): array
    {
        $params = request()->param();
        $id = $params['id'] ?? "";
        if (empty($id)) {
            return ['code' => 1, 'msg' => '请选择删除项'];
        }
        $result = $this->model->where('id', $id)->delete();
        if (!$result) {
            return ['code' => 1, 'msg' => '删除失败'];
        }
        return ['code' => 0, 'msg' => '删除成功'];
    }

}